package com.freeseed.model.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.freeseed.model.entity.Enrollment;
import com.freeseed.model.mapper.EnrollmentMapper;
import com.freeseed.model.service.impl.EnrollmentServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Allwayz
 * @since 2020-03-20
 */
@Slf4j
@RestController
@RequestMapping("enrollments")
public class EnrollmentController {
    @Autowired
    private EnrollmentMapper enrollmentMapper;

    @GetMapping
    public List<Enrollment> getEnrollmentList(){
        List<Enrollment> enrollmentList = enrollmentMapper.selectList(new QueryWrapper<>());
        return enrollmentList;
    }

    @GetMapping("{Id}")
    public Enrollment getEnrollmentById(@PathVariable int Id){
        Enrollment enrollment =  enrollmentMapper.selectById(Id);
        return enrollment;
    }

    @PostMapping
    public Enrollment addEnrollment(Enrollment enrollment){
        enrollmentMapper.insert(enrollment);
        return enrollment;
    }

    @PutMapping
    public Enrollment updateEnrollment(Enrollment enrollment){
        enrollmentMapper.update(enrollment,new UpdateWrapper<Enrollment>().eq("enrollment_id",enrollment.getEnrollmentId()));
        return enrollment;
    }

    @DeleteMapping("{Id}")
    public void deleteEnrollment(@PathVariable int Id){
        enrollmentMapper.deleteById(Id);
    }
}
